package com.goldplusgold.tradedelay.app.home.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.goldplusgold.support.lib.base.fragment.BaseFragment;
import com.goldplusgold.support.lib.widget.AsyncTask;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshBase;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshListView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.adapter.ExpertCommentAdapter;
import com.goldplusgold.tradedelay.app.home.serivce.HomeService;
import com.goldplusgold.tradedelay.app.home.pojo.Article;
import com.goldplusgold.tradedelay.app.home.pojo.ArticleList;
import com.goldplusgold.support.lib.widget.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by admin on 2017/5/13.
 */

public class ExpertArticleFragment extends BaseFragment implements View.OnClickListener, Handler.Callback, PullToRefreshListView.OnRefreshListener2 {

    @BindView(R.id.id_title_view_text_view)
    TitleView mTitleViewTextView;
    @BindView(R.id.id_list_view_articles)
    PullToRefreshListView mListViewArticles;
    private ExpertCommentAdapter mArticleListAdapter;
    private Handler mHandler = new Handler(this);

    private int mPageIndex = 0;
    private int mPageSize = 10;
    private final int NETWORK_ERROR_CODE = 0x01;

    private boolean isPullDown = false;
    private boolean isPullUp = false;


    public static ExpertArticleFragment newInstance() {
        Bundle args = new Bundle();
        ExpertArticleFragment fragment = new ExpertArticleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_expert_article;
    }


    @Override
    protected void initView() {
        super.initView();
        mTitleViewTextView.getBackView().setVisibility(View.VISIBLE);
        mTitleViewTextView.getBackView().setOnClickListener(this);
        mTitleViewTextView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleViewTextView.getTitletextView().setText(R.string.expert_judgements);
        mArticleListAdapter = new ExpertCommentAdapter(getActivity());
        mListViewArticles.setAdapter(mArticleListAdapter);
        mListViewArticles.setOnItemClickListener(mArticleListAdapter);
        mListViewArticles.setOnRefreshListener(this);
        mListViewArticles.getLoadingLayoutProxy().setPullLabel("");
        mListViewArticles.getLoadingLayoutProxy().setRefreshingLabel("");
        mListViewArticles.getLoadingLayoutProxy().setReleaseLabel("");
        mListViewArticles.getLoadingLayoutProxy().setLastUpdatedLabel("");
        mListViewArticles.setMode(PullToRefreshBase.Mode.BOTH);
        new LoadArticleAsyncTask().execute(AsyncTask.TYPE_NET);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        if (isPullUp == true) {
            return;
        }
        mPageIndex = 0;
        isPullDown = true;
        new LoadArticleAsyncTask().execute(AsyncTask.TYPE_NET);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        if (isPullDown == true) {
            return;
        }
        mPageIndex++;
        isPullUp = true;
        new LoadArticleAsyncTask().execute(AsyncTask.TYPE_NET);
    }

    @Override
    public boolean handleMessage(Message msg) {
        int type = msg.what;
        switch (type) {
            case NETWORK_ERROR_CODE:
                showToastMessage(R.string.network_error, Toast.LENGTH_LONG);
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
        }
    }


    public class LoadArticleAsyncTask extends AsyncTask<Void, Void, List<Article>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (!isPullUp && !isPullDown && mPageIndex == 0) {
                showLoadingControl();
            }
        }

        @Override
        protected List<Article> doInBackground(Void... params) {

            try {
                int offset = mPageIndex * mPageSize;
                ArticleList articleList = HomeService.getInstance().getArticleList(offset, mPageSize, "0");
                if (articleList != null) {
                    return articleList.getArticleList();
                }
            } catch (Exception e) {
                e.printStackTrace();
                sendNetworkErrorMessage();
            }


            return null;
        }

        @Override
        protected void onPostExecute(List<Article> articleList) {
            super.onPostExecute(articleList);
            if (!isAvailableActivity()) {
                return;
            }
            dismisLoadingControl();
            mListViewArticles.onRefreshComplete();
            isPullDown = false;
            isPullUp = false;

            if (articleList == null || articleList.isEmpty()) {
                return;
            }
            if (mPageIndex > 0) {
                mArticleListAdapter.addArrayList(new ArrayList(articleList));
            } else {
                mArticleListAdapter.setArrayList(new ArrayList(articleList));
            }

        }
    }

    private void sendNetworkErrorMessage() {
        Message message = mHandler.obtainMessage();
        message.what = NETWORK_ERROR_CODE;
        mHandler.sendMessage(message);
    }

}
